local lsp_maoding = fk.CreateSkill {
    name = "lsp_maoding",
    dynamic_desc = function(self, player)
        local n = math.max(1, player:getMark("@lsp_lingying"))
        local x = math.max(1, player:getMark("@lsp_lingying") - 2)
        local z = math.max(1, player:getMark("@lsp_lingying") - 3)
        if player:usedSkillTimes("lsp_maoding", player.HistoryPhase) == 0 then
            return "失去一点盈能，对一名角色造成一点伤害，其视为对你使用一张【杀】，若此【杀】未造成伤害，你摸两张牌，然后你可以令其摸" .. x .. "张牌"
        elseif player:usedSkillTimes("lsp_maoding", player.HistoryPhase) == 1 then
            return "失去两点盈能，此阶段至其他角色的距离、可使用【杀】的次数+" .. n .. "，此阶段内每造成一点伤害，回复一点盈能"
        elseif player:usedSkillTimes("lsp_maoding", player.HistoryPhase) == 2 then
            return "失去三点盈能，获得" .. z .. "张普通锦囊牌或装备牌，然后你可以将至多等量的牌交给一名其他角色，其可以使用其中一张"
        end
    end,
}

Fk:loadTranslationTable {
    ["lsp_maoding"] = "锚定",
    [":lsp_maoding"] = "出牌阶段限三次，若你有盈能，每次发动该技能时，依次执行下述效果(X为盈能数，至少为1)：<br>" ..
        "--失去一点盈能，对一名角色造成一点伤害，其视为对你使用一张【杀】，若此【杀】未造成伤害，你摸两张牌，然后你可以令其摸X-2张牌；<br>" ..
        "--失去两点盈能，此阶段至其他角色的距离、可使用【杀】的次数+X，此阶段内每造成一点伤害，回复一点盈能；<br>" ..
        "--失去三点盈能，获得X张普通锦囊牌或装备牌，然后你可以将至多等量的牌交给一名其他角色，其可以使用其中一张。"
}

lsp_maoding:addEffect("active", {
    anim_type = "control",
    prompt = function(self, player)
        if player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 0 then
            return "锚定：失去一点盈能，打别人一下，别人杀你，没杀到你摸" ..
                math.max(1, player:getMark("@lsp_lingying")) ..
                "牌，你也可以让他摸" .. math.max(1, player:getMark("@lsp_lingying") - 2) .. "张牌"
        elseif player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 1 then
            return "锚定：失去两点盈能，额外用" .. math.max(1, player:getMark("@lsp_lingying") - 2) .. "次杀，造成伤害回盈能"
        elseif player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 2 then
            return "锚定：失去三点盈能，摸" .. math.max(1, player:getMark("@lsp_lingying") - 3) .. "张锦囊或者装备，可以给别人"
        end
        return ""
    end,
    card_num = 0,
    target_num = 0,
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
    can_use = function(self, player)
        if player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 0 then
            return player:getMark("@lsp_lingying") > 0
        elseif player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 1 then
            return player:getMark("@lsp_lingying") > 1
        elseif player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 2 then
            return player:getMark("@lsp_lingying") > 2
        end
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        local n = math.max(1, player:getMark("@lsp_lingying"))
        if player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 1 then
            room:removePlayerMark(player, "@lsp_lingying", 1)
            local to = room:askToChoosePlayers(player, {
                min_num = 1,
                max_num = 1,
                targets = room:getOtherPlayers(player),
                skill_name = lsp_maoding.name,
                prompt = "锚定：请选择对其造成伤害的角色",
                cancelable = false,
            })
            if #to > 0 then
                room:damage({
                    from = player,
                    to = to[1],
                    damage = 1,
                    damageType = fk.NormalDamage,
                    skillName = lsp_maoding.name
                })
                if not to[1].dead then
                    local use = room:useVirtualCard("slash", nil, to[1], player, lsp_maoding.name, true)
                    if use then
                        if not use.damageDealt then
                            player:drawCards(2, lsp_maoding.name)
                            if room:askToSkillInvoke(player, { skill_name = lsp_maoding.name, prompt = "锚定：是否令其摸" .. n .. "张牌" }) and player:getMark("@lsp_lingying") > 2 then
                                to[1]:drawCards(n - 2, lsp_maoding.name)
                            end
                        end
                    end
                end
            end
        end
        if player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 2 then
            room:removePlayerMark(player, "@lsp_lingying", 2)
            if player:getMark("@lsp_lingying") > 0 then
                room:setPlayerMark(player, "lsp_maoding_skill2-phase", n)
                room:addPlayerMark(player, MarkEnum.SlashResidue .. "-phase", n)
            end
        end
        if player:usedSkillTimes(lsp_maoding.name, player.HistoryPhase) == 3 then
            room:removePlayerMark(player, "@lsp_lingying", 3)
            if player:getMark("@lsp_lingying") > 0 then
                local cards = room:getCardsFromPileByRule(".|.|.|.|.|trick,equip", n)
                if #cards > 0 then
                    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, lsp_maoding.name, nil,
                        false, player)
                    local to, ids = room:askToChooseCardsAndPlayers(player, {
                        min_card_num = 1,
                        max_card_num = #cards,
                        min_num = 1,
                        max_num = 1,
                        targets = room:getOtherPlayers(player, false),
                        skill_name = lsp_maoding.name,
                        prompt = "锚定：你可以将至多" .. #cards .. "张牌交给一名其他角色，其可以使用其中一张",
                        cancelable = true,
                        will_throw = false,
                    })
                    if #to > 0 and #ids > 0 then
                        room:moveCardTo(ids, Card.PlayerHand, to[1], fk.ReasonGive, lsp_maoding.name, nil, false, player)
                        room:askToUseRealCard(to[1], {
                            pattern = ids,
                            skill_name = lsp_maoding.name,
                            prompt = "锚定：你可以使用其中一张牌。",
                            extra_data = {
                                bypass_times = true,
                                extraUse = true,
                            }
                        })
                    end
                end
            end
        end
        if player:getMark("@lsp_lingying") == 0 then
            local isDeputy = true
            if player.general == "lsp__liyue_ex" then
                isDeputy = false
            end
            room:changeHero(player, "lsp__liyue", false, isDeputy, true, false)
            room:setPlayerMark(player, "lsp_maoding_skill2-phase", 0)
            room:setPlayerMark(player, MarkEnum.SlashResidue .. "-phase", 0)
        end
    end,
})

lsp_maoding:addEffect(fk.Damage, {
    can_refresh = function(self, event, target, player, data)
        return target == player and player:hasSkill(lsp_maoding.name) and player:getMark("lsp_maoding_skill2-phase") > 0 and
            player:getMark("@lsp_lingying") < 5
    end,
    on_refresh = function(self, event, target, player, data)
        if player:getMark("@lsp_lingying") < 5 then
            player.room:addPlayerMark(player, "@lsp_lingying", data.damage)
        end
    end,
})
lsp_maoding:addEffect("distance", {
    correct_func = function(self, from, to)
        if from:hasSkill(lsp_maoding.name) and from:getMark("lsp_maoding_skill2-phase") > 0 then
            return -from:getMark("lsp_maoding_skill2-phase")
        end
    end,
})

return lsp_maoding
